﻿// -----------------------------------------------------------------------
// <copyright file="Updater.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Metal.Data
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Core.Player;

    /// <summary>
    /// Updates some internal variables
    /// </summary>
    public class Updater
    {
        /// <summary> A timer that updates some data </summary>
        private System.Timers.Timer updateTimer;

        /// <summary>
        /// Initializes a new instance of the Updater class.
        /// </summary>
        public Updater()
        {
            // Player events
            Events.Instance.Play += new EventHandler(this.OnPlay);
            Events.Instance.Stop += new EventHandler(this.OnStop);
            Events.Instance.Pause += new EventHandler(this.OnStop);
            Events.Instance.Resume += new EventHandler(this.OnResume);
            Events.Instance.RadioSongChanged += new EventHandler(this.OnPlay);

            // Timer updater
            this.updateTimer = new System.Timers.Timer();
            this.updateTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerTick);
            this.updateTimer.Interval = 500;
        }

        /// <summary>
        /// Sets some properties when starting playback
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void OnPlay(object sender, EventArgs e)
        {
            if (Core.Player.MediaInfo.FileName != null)
            {
                DataStorage.SetProperty("FileName", Core.Player.MediaInfo.FileName);
                int bitRate = -1;

                if (Core.Player.MediaInfo.BitRate != string.Empty)
                {
                    bitRate = Convert.ToInt32(Core.Player.MediaInfo.BitRate);
                }

                // Make some corrections
                if (bitRate < 0)
                {
                    DataStorage.SetProperty("Kbps", "128 kbps");
                }
                else
                {
                    DataStorage.SetProperty("Kbps", Core.Player.MediaInfo.BitRate + " kbps");
                }

                DataStorage.SetProperty("Album", Core.Player.MediaInfo.Album);
                DataStorage.SetProperty("Artist", Core.Player.MediaInfo.Artist);
                DataStorage.SetProperty("Comment", Core.Player.MediaInfo.Comment);
                DataStorage.SetProperty("FilePath", Core.Player.MediaInfo.FilePath);
                DataStorage.SetProperty("Genre", Core.Player.MediaInfo.Genre);
                DataStorage.SetProperty("RadioChannelInfo", Core.Player.MediaInfo.RadioChannelInfo);
                DataStorage.SetProperty("Title", Core.Player.MediaInfo.Title);
                DataStorage.SetProperty("Track", Core.Player.MediaInfo.Track);
                DataStorage.SetProperty("Year", Core.Player.MediaInfo.Year);

                DataStorage.SetProperty("Frequency", Core.Player.MediaInfo.Frequency);
                DataStorage.SetProperty("Bytes", Core.Player.MediaInfo.Bytes);
                DataStorage.SetProperty("Channel", Core.Player.MediaInfo.Channel);
            }

            this.updateTimer.Start();
        }

        /// <summary>
        /// Stops the timer when the playback is stopped
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void OnStop(object sender, EventArgs e)
        {
            this.updateTimer.Stop();
        }

        /// <summary>
        /// Resumes the timer when the playback is resumed
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void OnResume(object sender, EventArgs e)
        {
            this.updateTimer.Start();
        }

        /// <summary>
        /// Update values
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void TimerTick(object sender, EventArgs e)
        {
            DataStorage.SetProperty("CurrentTime", Core.Player.Status.CurrentPositionFormated);
            DataStorage.SetProperty("TotalTime", Core.Player.Status.TotalTimeFormated);
            DataStorage.SetProperty("TrackBarValue", Core.Player.Status.CurrentPositionInSeconds.ToString());
        }
    }
}
